/**
 * Created by 768138386@qq.com on 2017/7/20.
 */
angular.module('data.services', [])
    .service('dataService', dataService);


function dataService($http, $q) {

    return {
        get: get,
        post: post,
        put: put,
        del: del
    };

    function success(resp) {
        return $q.resolve(resp.data);
    }

    function failure(error) {
        return $q.reject(error);
    }

    function get(address, qParams, shouldCache) {

        var config = {
            method: 'GET',
            url: address,
            params: qParams || null,
            cache: shouldCache || false
        };
        return $http(config).then(success).catch(failure);
    }

    function post(address, data) {
        var config = {
            method: 'POST',
            url: address,
            data: data
        };
        return $http(config).then(success).catch(failure);
    }

    function put(address, data) {
        return $http.put(address, data).then(success).catch(failure);

    }

    function del(address,data) {
        return $http.delete(address).then(success).catch(failure);
    }

}